package me.siphone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import com.tencent.mmkv.MMKV;
import me.siphone.Constants;
import me.siphone.sip.SipUtil;
import org.linphone.R;
import org.linphone.service.LinphoneService;
import org.linphone.service.ServiceWaitThread;
import org.linphone.service.ServiceWaitThreadListener;

public class SplashActivity extends BaseActivity implements ServiceWaitThreadListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (LinphoneService.isReady()) {
            onServiceReady();
        } else {
            Log.d(Constants.TAG, "启动服务");
            try {
                startService(new Intent().setClass(this, LinphoneService.class));
                new ServiceWaitThread(this).start();
            } catch (IllegalStateException ise) {
                Log.e("Linphone", "Exception raised while starting service: " + ise);
            }
        }
    }

    @Override
    public void onServiceReady() {
        Log.d(Constants.TAG, "服务启动完成");
        SipUtil.registerListener();
        String token = MMKV.defaultMMKV().getString(Constants.TOKEN, "");
        if (TextUtils.isEmpty(token)) {
            startActivity(new Intent(this, LoginActivity.class));

            //            startActivity(new Intent(this, GenericConnectionAssistantActivity.class));

        } else {
            startActivity(new Intent(this, MainActivity.class));

            //            Intent intent = new Intent(this, DialerActivity.class);
            //            intent.addFlags(
            //                    Intent.FLAG_ACTIVITY_NO_ANIMATION |
            // Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            //            startActivity(intent);
        }
        finish();
    }
}
